package com.yaorange.teaching.listener;

import com.yaorange.teaching.bo.InterviewListenerBo;
import com.yaorange.teaching.domain.YaHighInterview;
import com.yaorange.teaching.index.HighInterviewIndex;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2024/12/6
 */
@Component
public class InterviewListener {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
/*    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "edu_smart.interview.es.queue"),
            exchange = @Exchange(name = "edu_smart.interview.exchange", type = ExchangeTypes.TOPIC),
            key = {"interview.add","interview.update"}
    ))
    public void listen(YaHighInterview item) {
        HighInterviewIndex highInterviewIndex = new HighInterviewIndex(item.getId(), item.getStage(), item.getQuestion(), item.getAnswer(), item.getKnowledgePoint(), item.getImportance());
        elasticsearchRestTemplate.save(highInterviewIndex);
    }*/


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "queue.canal"),
            exchange = @Exchange(name = "exchange.fanout.canal", type = ExchangeTypes.TOPIC),
            key = {"example"}
    ))
    public void listen(InterviewListenerBo bo) {
        System.out.println(bo);
        String type = bo.getType();
        if ("DELETE".equals(type)) {
            bo.getData().forEach(item -> elasticsearchRestTemplate.delete(item.getId().toString(), HighInterviewIndex.class));
        } else {
//            bo.getData().forEach(item->{
//                    HighInterviewIndex highInterviewIndex = new HighInterviewIndex(item.getId(), item.getStage(), item.getQuestion(), item.getAnswer(), item.getKnowledgePoint(), item.getImportance())
//                    elasticsearchRestTemplate.save(highInterviewIndex);
//                }
//            );
        }
    }
}
